##########################################################################################
# genome geome alignment Makefile

.DELETE_ON_ERROR:
CMD_SUBMIT=@qrsh -now n -cwd -q short_jobs.q -p -5


#
# Make in three parts. Second part depends on first etc
#
all: do_second_part


clean: cleandirectories


PROT_SEQ1?=prot_1.fa
CDNA_SEQ1?=cdna_1.fa
PROT_SEQ2?=prot_2.fa
CDNA_SEQ2?=cdna_2.fa

##########################################################################################
#
#   Make directories
#
makedirectories: ortholog_pairs

cleandirectories:
	@echo "	Make ortholog_pairs directory"
	@rm -rf ortholog_pairs/*

ortholog_pairs:
	@echo "	Make ortholog_pairs directory"
	@mkdir -p ortholog_pairs


##########################################################################################
#
#   First Part:
#
#           get all orthologues
#
#
do_first_part: makedirectories
	@echo "- First Part"
	@echo "	Get all orthologue pairs from psql..."
	@echo "	break into chunks of 500..."
	@echo "	****************************************************************************"
	@echo
	@$(MAKE) -s first_part
	@echo "- First Part Finished"
	@echo
	@echo

first_part: ortholog_pairs/0.id_pairs



#_________________________________________________________________________________________
#
#   break up gene pairs into chunks of 1000 in preparation of andreas's script
#
ortholog_pairs/0.id_pairs: all.orthologs.gene_pairs
	@echo "	Break into chunks of 500..."
	@echo "	Clear ortholog_pairs directory of previous results"
	@rm -f $(CURDIR)/*.id_pairs
	@/net/cpp-group/Leo/bin/break_into_chunks.pl \
        --every 500 \
        --base_name "$(CURDIR)/ortholog_pairs/gene_ids.pairs" \
                      < all.orthologs.gene_pairs
	@echo


















##########################################################################################
#
#   Second Part:
#
#
#   Realign without seg
do_second_part: do_first_part
	@echo "- Second Part"
	@echo "	Run andreas script to align and map exon boundaries onto protein sequence..."
	@echo "	****************************************************************************"
	@echo
	@$(MAKE) -s second_part
	@echo "- Second Part Finished"
	@echo
	@echo

PAIRS_FILES=$(wildcard $(CURDIR)/ortholog_pairs/gene_ids.*.pairs)
EXON_MAPPED_FILES=$(PAIRS_FILES:%.pairs=%.exon_mapped)
second_part: all.exon_results

#_________________________________________________________________________________________
#
#   Realign without seg
#
%.exon_mapped: %.pairs
	@echo "	map exon boundaries"
	@$(CMD_SUBMIT) -v PYTHONPATH=/net/cpp-group/lib/python \
				-v LD_LIBRARY_PATH=/net/cpp-group/lib  \
				-l arch=lx24-x86 \
				/net/cpp-group/bin/python $(CURDIR)/src/orthologs2transcripts.py \
				--map1=genes2transcripts_1.map \
				--map2=genes2transcripts_2.map \
				--peptides1=$(PROT_SEQ1) \
				--peptides2=$(PROT_SEQ2) \
				--transcripts1=$(CDNA_SEQ1) \
				--transcripts2=$(CDNA_SEQ2) \
				--cds1=gene_structure_1.cds \
				--cds2=gene_structure_2.cds \
				--max-intron-length=20000 \
				--write-exons=exons \
				--boundaries-max-slippage=9 \
				--verbose=3 \
				--boundaries-max-missed=1 \
				--boundaries-allow-missed=3 \
				--missing-max-missing=1 \
				--missing-min-present=3 \
				--min-coverage=1 \
				< $< > $@
	@echo "	map finish"

#_________________________________________________________________________________________
#
#   concatenate all
#
all.exon_results: $(EXON_MAPPED_FILES)
	@echo "	Analyse exon map results"
	./parse_ortholog_exon_results.pl $(EXON_MAPPED_FILES)
	@echo "	Finished"
	@echo



